home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / list_visuals.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  6.1 KB  |  240 lines

  1. //
  2. // "$Id: list_visuals.cxx,v 1.5 1999/01/07 19:17:57 mike Exp $"
  3. //
  4. // Visual list utility for the Fast Light Tool Kit (FLTK).
  5. //
  6. // List all the visuals on the screen, and dumps anything interesting
  7. // about them to stdout.
  8. //
  9. // Does not use fltk.
  10. //
  11. // This file may be #included in another program to make a function to
  12. // call to list the visuals.  Fl.H must be included first to indicate this.
  13. //
  14. // Copyright 1998-1999 by Bill Spitzak and others.
  15. //
  16. // This library is free software; you can redistribute it and/or
  17. // modify it under the terms of the GNU Library General Public
  18. // License as published by the Free Software Foundation; either
  19. // version 2 of the License, or (at your option) any later version.
  20. //
  21. // This library is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24. // Library General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU Library General Public
  27. // License along with this library; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  29. // USA.
  30. //
  31. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  32. //
  33.  
  34. #ifdef WIN32
  35. #include <FL/Fl.H>
  36. #include <FL/fl_message.H>
  37.  
  38. int main(int, char**) {
  39.   fl_alert("Currently, this program works only under X.");
  40.   return 1;
  41. }
  42.  
  43. #else
  44.  
  45. #include <config.h>
  46.  
  47. #ifndef Fl_H
  48.  
  49. #include <X11/Xlib.h>
  50. #include <X11/Xutil.h>
  51. #include <X11/Xatom.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54.  
  55. Display *fl_display;
  56. int fl_screen;
  57. const char *dname;
  58. void fl_open_display() {
  59.   fl_display = XOpenDisplay(dname);
  60.   if (!fl_display) {
  61.     fprintf(stderr,"Can't open display: %s\n",XDisplayName(dname));
  62.     exit(1);
  63.   }
  64.   fl_screen = DefaultScreen(fl_display);
  65. }
  66.  
  67. #endif
  68.  
  69. const char *ClassNames[] = {
  70.   "StaticGray ",
  71.   "GrayScale  ",
  72.   "StaticColor",
  73.   "PseudoColor",
  74.   "TrueColor  ",
  75.   "DirectColor"
  76. };
  77.  
  78. // SERVER_OVERLAY_VISUALS property element:
  79. typedef struct _OverlayInfo {
  80.   long overlay_visual;
  81.   long transparent_type;
  82.   long value;
  83.   long layer;
  84. } OverlayInfo;
  85.  
  86. #if HAVE_MULTIBUF
  87. #include <X11/extensions/multibuf.h>
  88. #endif
  89.  
  90. #if HAVE_XDBE
  91. #include <X11/extensions/Xdbe.h>
  92. #endif
  93.  
  94. static void print_mask(XVisualInfo* p) {
  95.   int n = 0;
  96.   int what = 0;
  97.   int print_anything = 0;
  98.   char buf[20];
  99.   char *q = buf;
  100.   *q = 0;
  101.   int b; unsigned int m; for (b=32,m=0x80000000; ; b--,m>>=1) {
  102.     int new_what = 0;
  103.     if (p->red_mask&m) new_what = 'r';
  104.     else if (p->green_mask&m) new_what = 'g';
  105.     else if (p->blue_mask&m) new_what = 'b';
  106.     else new_what = '?';
  107.     if (new_what != what) {
  108.       if (what && (what != '?' || print_anything)) {
  109.     q += sprintf(q,"%d%c", n, what);
  110.     print_anything = 1;
  111.       }
  112.       what = new_what;
  113.       n = 1;
  114.     } else {
  115.       n++;
  116.     }
  117.     if (!b) break;
  118.   }
  119.   printf("%7s", buf);
  120. }
  121.  
  122. void list_visuals() {
  123.   fl_open_display();
  124.   XVisualInfo vTemplate;
  125.   int num;
  126.   XVisualInfo *visualList = XGetVisualInfo(fl_display,0,&vTemplate,&num);
  127.  
  128.   XPixmapFormatValues *pfvlist;
  129.   static int numpfv;
  130.   pfvlist = XListPixmapFormats(fl_display, &numpfv);
  131.  
  132.   OverlayInfo *overlayInfo = 0;
  133.   int numoverlayinfo = 0;
  134.   Atom overlayVisualsAtom = XInternAtom(fl_display,"SERVER_OVERLAY_VISUALS",1);
  135.   if (overlayVisualsAtom) {
  136.     unsigned long sizeData, bytesLeft;
  137.     Atom actualType;
  138.     int actualFormat;
  139.     if (!XGetWindowProperty(fl_display, RootWindow(fl_display, fl_screen),
  140.                overlayVisualsAtom, 0L, 10000L, False,
  141.                overlayVisualsAtom, &actualType, &actualFormat,
  142.                &sizeData, &bytesLeft,
  143.                (unsigned char **) &overlayInfo))
  144.       numoverlayinfo = int(sizeData/4);
  145.   }
  146.  
  147. #if HAVE_MULTIBUF
  148.   int event_base, error_base;
  149.   XmbufBufferInfo *mbuf, *sbuf;
  150.   int nmbuf = 0, nsbuf = 0;
  151.   if (XmbufQueryExtension(fl_display,&event_base, &error_base)) {
  152.     XmbufGetScreenInfo(fl_display,RootWindow(fl_display,fl_screen),
  153.                &nmbuf, &mbuf, &nsbuf, &sbuf);
  154.   }
  155. #endif
  156.  
  157. #if HAVE_XDBE
  158.   int event_base, error_base;
  159.   int numdouble = 0;
  160.   XdbeVisualInfo *dbe = 0;
  161.   if (XdbeQueryExtension(fl_display, &event_base, &error_base)) {
  162.     Drawable root = RootWindow(fl_display,fl_screen);
  163.     int numscreens = 1;
  164.     XdbeScreenVisualInfo *a = XdbeGetVisualInfo(fl_display,&root,&numscreens);
  165.     if (!a) printf("error getting double buffer visuals\n");
  166.     else {
  167.       dbe = a->visinfo;
  168.       numdouble = a->count;
  169.     }
  170.   }
  171. #endif
  172.  
  173.   for (int i=0; i<num; i++) {
  174.     XVisualInfo *p = visualList+i;
  175.  
  176.     XPixmapFormatValues *pfv;
  177.     for (pfv = pfvlist; ; pfv++) {
  178.       if (pfv >= pfvlist+numpfv) {pfv = 0; break;} // should not happen!
  179.       if (pfv->depth == p->depth) break;
  180.     }
  181.  
  182.     int j = pfv ? pfv->bits_per_pixel : 0;
  183.     printf(" %2ld: %s %2d/%d", p->visualid, ClassNames[p->c_class],
  184.        p->depth, j);
  185.     if (j < 10) putchar(' ');
  186.  
  187.     print_mask(p);
  188.  
  189.     for (j=0; j<numoverlayinfo; j++) {
  190.       OverlayInfo *o = &overlayInfo[j];
  191.       if (o->overlay_visual == long(p->visualid)) {
  192.     printf(" overlay(");
  193.     if (o->transparent_type==1) printf("transparent pixel %ld, ",o->value);
  194.     else if (o->transparent_type==2) printf("transparent mask %ld, ",o->value);
  195.     else printf("opaque, ");
  196.     printf("layer %ld)", o->layer);
  197.       }
  198.     }
  199.  
  200. #if HAVE_MULTIBUF
  201.     for (j=0; j<nmbuf; j++) {
  202.       XmbufBufferInfo *m = &mbuf[j];
  203.       if (m->visualid == p->visualid)
  204.     printf(" multibuffer(%d)", m->max_buffers);
  205.     }
  206.     for (j=0; j<nsbuf; j++) {
  207.       XmbufBufferInfo *m = &sbuf[j];
  208.       if (m->visualid == p->visualid)
  209.     printf(" stereo multibuffer(%d)", m->max_buffers);
  210.     }
  211. #endif
  212.  
  213. #if HAVE_XDBE
  214.     for (j = 0; j < numdouble; j++) if (dbe[j].visual == p->visualid)
  215.       printf(" doublebuf(perflevel %d)",dbe[j].perflevel);
  216. #endif
  217.  
  218.     if (p->visualid==XVisualIDFromVisual(DefaultVisual(fl_display,fl_screen)))
  219.       printf(" (default visual)");
  220.  
  221.     putchar('\n');
  222.   }
  223. }
  224.  
  225. #endif
  226.  
  227. #ifndef Fl_H
  228. int main(int argc, char **argv) {
  229.   if (argc == 1);
  230.   else if (argc == 2 && argv[1][0]!='-') dname = argv[1];
  231.   else {fprintf(stderr,"usage: %s <display>\n",argv[0]); exit(1);}
  232.   list_visuals();
  233.   return 0;
  234. }
  235. #endif
  236.  
  237. //
  238. // End of "$Id: list_visuals.cxx,v 1.5 1999/01/07 19:17:57 mike Exp $".
  239. //
  240.